<template>
  <div class="bookwithuser-container">
    <h1 class="page-title">借阅状态</h1>

    <!-- 搜索表单 -->
    <el-card class="search-card">
      <el-form inline="true" size="small" class="search-form">
        <el-form-item label="图书编号">
          <el-input v-model="search1" placeholder="请输入图书编号" clearable>
            <template #prefix><el-icon class="el-input__icon"><search/></el-icon></template>
          </el-input>
        </el-form-item>
        <el-form-item label="图书名称">
          <el-input v-model="search2" placeholder="请输入图书名称" clearable>
            <template #prefix><el-icon class="el-input__icon"><search /></el-icon></template>
          </el-input>
        </el-form-item>
        <el-form-item label="借阅者" v-if="user.role == 1">
          <el-input v-model="search3" placeholder="请输入借阅者昵称" clearable>
            <template #prefix><el-icon class="el-input__icon"><search /></el-icon></template>
          </el-input>
        </el-form-item>
        <div class="form-buttons">
          <el-button type="primary" @click="load" size="mini" class="search-button">
            <i class="el-icon-search"></i> 查询
          </el-button>
          <el-button size="mini" type="danger" @click="clear" class="reset-button">
            <i class="el-icon-refresh"></i> 重置
          </el-button>
        </div>
      </el-form>
    </el-card>
    
    <!-- 操作按钮区 -->
    <div class="action-bar" v-if="user.role == 1">
      <el-popconfirm title="确认删除?" @confirm="deleteBatch">
        <template #reference>
          <el-button type="danger" size="mini" class="batch-delete-button">
            <i class="el-icon-delete"></i> 批量删除
          </el-button>
        </template>
      </el-popconfirm>
    </div>
    
    <!-- 数据表格 -->
    <el-card class="data-card">
      <el-table 
        :data="tableData" 
        stripe 
        border="true" 
        @selection-change="handleSelectionChange"
        class="data-table"
      >
        <el-table-column v-if="user.role ==1" type="selection" width="55"></el-table-column>
        <el-table-column prop="isbn" label="图书编号" sortable />
        <el-table-column prop="bookName" label="图书名称" />
        <el-table-column prop="nickName" label="借阅者" />
        <el-table-column prop="lendtime" label="借阅时间" />
        <el-table-column prop="deadtime" label="最迟归还日期" />
        <el-table-column prop="prolong" label="可续借次数">
          <template v-slot="scope">
            <el-tag :type="scope.row.prolong > 0 ? 'success' : 'info'" size="small">
              {{ scope.row.prolong }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="180">
          <template v-slot="scope">
            <div class="table-action-buttons">
              <el-button type="primary" size="mini" @click="handleEdit(scope.row)" v-if="user.role == 1" class="edit-button">
                <i class="el-icon-edit"></i> 修改
              </el-button>
              <el-popconfirm title="确认删除?" @confirm="handleDelete(scope.row)" v-if="user.role == 1">
                <template #reference>
                  <el-button type="danger" size="mini" class="delete-button">
                    <i class="el-icon-delete"></i> 删除
                  </el-button>
                </template>
              </el-popconfirm>
              <el-popconfirm 
                title="确认续借(续借一次延长30天)?" 
                @confirm="handlereProlong(scope.row)" 
                v-if="user.role == 2" 
                :disabled="scope.row.prolong == 0"
              >
                <template #reference>
                  <el-button 
                    type="success" 
                    size="mini" 
                    :disabled="scope.row.prolong == 0" 
                    class="prolong-button"
                  >
                    <i class="el-icon-refresh"></i> 续借
                  </el-button>
                </template>
              </el-popconfirm>
            </div>
          </template>
        </el-table-column>
      </el-table>
      
      <!-- 分页 -->
      <el-pagination
        v-model:currentPage="currentPage"
        :page-sizes="[5, 10, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        class="pagination"
      >
      </el-pagination>
    </el-card>

    <!-- 编辑对话框 -->
    <el-dialog v-model="dialogVisible2" title="修改借阅信息" width="30%" class="bookwithuser-dialog">
      <el-form :model="form" label-width="120px" class="edit-form">
        <el-form-item label="图书编号">
          <el-input v-model="form.isbn" class="form-input"></el-input>
        </el-form-item>
        <el-form-item label="图书名称">
          <el-input v-model="form.bookName" class="form-input"></el-input>
        </el-form-item>
        <el-form-item label="借阅者">
          <el-input v-model="form.nickName" class="form-input"></el-input>
        </el-form-item>
        <el-form-item label="续借次数">
          <el-input v-model="form.prolong" class="form-input"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible2 = false" class="cancel-button">取 消</el-button>
          <el-button type="primary" @click="save" class="confirm-button">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
// @ is an alias to /src
import request from "../utils/request";
import {ElMessage} from "element-plus";
import moment from "moment";
export default {
  created(){
    let userStr = sessionStorage.getItem("user") ||"{}"
    this.user = JSON.parse(userStr)
    this.load()
  },
  name: 'bookwithuser',
  methods: {

    handleSelectionChange(val){
      this.forms = val
    },
    deleteBatch(){
      if (!this.forms.length) {
        ElMessage.warning("请选择数据！")
        return
      }
    //  一个小优化，直接发送这个数组，而不是一个一个的提交删除
      request.post("bookwithuser/deleteRecords",this.forms).then(res =>{
        if(res.code === '0'){
          ElMessage.success("批量删除成功")
          this.load()
        }
        else {
          ElMessage.error(res.msg)
        }
      })
    },
    load(){
      if(this.user.role == 1){
        request.get("/bookwithuser",{
          params:{
            pageNum: this.currentPage,
            pageSize: this.pageSize,
            search1: this.search1,
            search2: this.search2,
            search3: this.search3,
          }
        }).then(res =>{
          console.log(res)
          this.tableData = res.data.records
          this.total = res.data.total
        })
      }
      else {
        request.get("/bookwithuser",{
          params:{
            pageNum: this.currentPage,
            pageSize: this.pageSize,
            search1: this.search1,
            search2: this.search2,
            search3: this.user.id,
          }
        }).then(res =>{
          console.log(res)
          this.tableData = res.data.records
          this.total = res.data.total
        })
      }
    },
    clear(){
      this.search1 = ""
      this.search2 = ""
      this.search3 = ""
      this.load()
    },
    handleDelete(row){
      const form3 = JSON.parse(JSON.stringify(row))
      request.post("bookwithuser/deleteRecord",form3).then(res =>{
        console.log(res)
        if(res.code == 0 ){
          ElMessage.success("删除成功")
        }
        else
          ElMessage.error(res.msg)
        this.load()
      })
    },
    handlereProlong(row){
      var nowDate = new Date(row.deadtime);
      nowDate.setDate(nowDate.getDate()+30);
      row.deadtime = moment(nowDate).format("yyyy-MM-DD HH:mm:ss");
      row.prolong = row.prolong -1;
      request.post("/bookwithuser",row).then(res =>{
        console.log(res)
        if(res.code == 0){
          ElMessage({
            message: '续借成功',
            type: 'success',
          })
        }
        else {
          ElMessage.error(res.msg)
        }
        this.load()
        this.dialogVisible2 = false
      })
    },
    save(){
      //ES6语法
      //地址,但是？IP与端口？+请求参数
      // this.form?这是自动保存在form中的，虽然显示时没有使用，但是这个对象中是有它的
        request.post("/bookwithuser",this.form).then(res =>{
          console.log(res)
          if(res.code == 0){
            ElMessage({
              message: '修改信息成功',
              type: 'success',
            })
          }
          else {
            ElMessage.error(res.msg)
          }
          this.load()
          this.dialogVisible2 = false
        })
    },

    handleEdit(row){
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogVisible2 = true
    },
    handleSizeChange(pageSize){
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum){
      this.pageNum = pageNum
      this.load()
    },

  },
  data() {
    return {
      form: {},
      form2:{},
      form3:{},
      dialogVisible: false,
      dialogVisible2: false,
      search1:'',
      search2:'',
      search3:'',
      total:10,
      currentPage:1,
      pageSize: 10,
      tableData: [],
      user:{},
      forms:[],
    }
  },
}
</script>

<style scoped>
.bookwithuser-container {
  padding: 20px;
}

.page-title {
  font-size: 24px;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 20px;
  border-left: 4px solid var(--primary-color);
  padding-left: 10px;
}

.search-card {
  margin-bottom: 20px;
  border-radius: var(--border-radius-medium);
  box-shadow: var(--box-shadow);
  border: none;
}

.search-form {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
}

.form-buttons {
  display: flex;
  gap: 10px;
  margin-left: 10px;
}

.action-bar {
  margin: 20px 0;
}

.batch-delete-button {
  background-color: var(--danger-color);
  border-color: var(--danger-color);
}

.data-card {
  border-radius: var(--border-radius-medium);
  box-shadow: var(--box-shadow);
  border: none;
  margin-bottom: 20px;
}

.data-table {
  margin-bottom: 20px;
}

.table-action-buttons {
  display: flex;
  gap: 8px;
}

.edit-button {
  background-color: var(--primary-color);
  border-color: var(--primary-color);
}

.delete-button {
  background-color: var(--danger-color);
  border-color: var(--danger-color);
}

.prolong-button {
  background-color: var(--success-color);
  border-color: var(--success-color);
}

.prolong-button:disabled {
  background-color: var(--info-color);
  border-color: var(--info-color);
  opacity: 0.7;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.bookwithuser-dialog :deep(.el-dialog__header) {
  background-color: var(--primary-color);
  color: white;
  padding: 15px 20px;
}

.bookwithuser-dialog :deep(.el-dialog__title) {
  color: white;
  font-weight: 600;
}

.bookwithuser-dialog :deep(.el-dialog__body) {
  padding: 20px;
}

.edit-form {
  margin-top: 20px;
}

.form-input {
  width: 100%;
}

.cancel-button {
  border-radius: var(--border-radius-small);
}

.confirm-button {
  border-radius: var(--border-radius-small);
  background-color: var(--primary-color);
  border-color: var(--primary-color);
}

:deep(.el-tag) {
  border-radius: 12px;
  padding: 0 10px;
  height: 24px;
  line-height: 24px;
  font-weight: 500;
}

:deep(.el-tag--success) {
  background-color: rgba(103, 194, 58, 0.1);
  border-color: rgba(103, 194, 58, 0.2);
  color: var(--success-color);
}

:deep(.el-tag--info) {
  background-color: rgba(144, 147, 153, 0.1);
  border-color: rgba(144, 147, 153, 0.2);
  color: var(--info-color);
}
</style>
